<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-19
 * Time: 18:39
 */

namespace app\admin\controller;


use app\src\admin\api\UserApi;
use app\src\admin\helper\AdminFunctionHelper;
use app\src\base\enum\StatusEnum;
use app\src\buyorder\logic\BuyOrderLogic;
use app\src\goods\logic\ProductLogic;
use app\src\insurance\action\InsuranceAction;
use app\src\insurance\logic\InsuranceApplyLogic;
use app\src\insurance\logic\InsuranceLogic;
use app\src\insurance\logic\InsuranceOrderInfoLogic;
use app\src\insurance\logic\InsuranceOrderLogic;
use app\src\message\config\MessageConfig;
use app\src\message\facade\MessageEntity;
use app\src\message\sms\JuheSms;
use app\src\powersystem\enum\AdminRoleEnum;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\admin\helper\ShortLinkHelper;
use app\src\powersystem\logic\AuthGroupLogic;
use app\src\session\logic\LoginSessionLogic;
use app\src\user\action\RegisterAction;
use app\src\user\action\RelationAction;
use app\src\user\action\UserLogoutAction;
use app\src\user\logic\MemberConfigLogic;
use app\src\user\logic\MemberLogic;
use app\src\user\logic\UcenterMemberLogic;
use app\src\user\logic\UserGroupLogic;
use app\src\user\logic\UserMemberLogic;
use app\src\user\logic\UserRelationLogic;
use app\src\user\logic\VUserInfoLogic;
use app\src\wallet\logic\WalletHisLogicV2;
use app\src\wallet\logic\WalletLogic;
use think\Db;
use think\Request;

class Insurance extends Admin{

    /*
     * 保险申请列表
     */
    public function ApplyList(){

        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            //查询未处理申请
            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $map = [
                'ia.apply_status' => 0
            ];
            $order = ['ia.create_time' => 'desc'];
            $result = (new InsuranceApplyLogic)->applyList($map, $page, $order);
            if($result['status']){
                $apply = $result['info'];
                $list = [];
                $InsuranceLogic = (new InsuranceLogic);
                foreach ($apply['list'] as $val){
                    //保险类型
                    $result = $InsuranceLogic->getInfo(['id' => $val['insurance_id']]);
                    if(!$result['status']) $this->error('获取保险申请列表失败', null, $result['info']);
                    $val['insurance_name'] = $result['info']['name'];

                    //是否已添加支付信息
                    $result = (new InsuranceOrderInfoLogic)->getInfo(['apply_id' => $val['id'], 'status' => 0]);
                    if(!$result['status']) $this->error('获取保险申请列表失败', null, $result['info']);
                    $val['has_pay_info'] = !empty($result['info']);

                    //是否首次购买
                    $result = (new InsuranceOrderLogic)->count(['uid' => $val['uid'], 'insurance_id' => $val['insurance_id'], 'status' => 0]);
                    if(!$result['status']) $this->error('获取保险申请列表失败', null, $result['info']);
                    $val['is_first'] = $result['info'] == 0;

                    $list[] = $val;

                }
                $apply['list'] = $list;
                $this->success('获取保险申请列表成功', null, $apply);
            }else{
                $this->error('获取保险申请列表失败', null, $result['info']);
            }
        }
    }

    /*
     * 保险申请列表
     */
    public function ApplyOverList(){

        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            //查询未处理申请
            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $map = [
                'ia.apply_status' => ['in', [1, 2]]
            ];
            $order = ['ia.create_time' => 'desc'];
            $result = (new InsuranceApplyLogic)->applyList($map, $page, $order);
            if($result['status']){
                $apply = $result['info'];
                $list = [];
                $InsuranceLogic = (new InsuranceLogic);
                foreach ($apply['list'] as $val){
                    //保险类型
                    $result = $InsuranceLogic->getInfo(['id' => $val['insurance_id']]);
                    if(!$result['status']) $this->error('获取保险申请列表失败', null, $result['info']);
                    $val['insurance_name'] = $result['info']['name'];

                    $list[] = $val;

                }
                $apply['list'] = $list;
                $this->success('获取保险申请列表成功', null, $apply);
            }else{
                $this->error('获取保险申请列表失败', null, $result['info']);
            }
        }
    }

    /*
     * 保险详情
     */
    public function ApplyOverDetail(){

        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');
            $insurance_id = $this->_param('insurance_id');
            if(empty($insurance_id)) $this->error('保险类型id缺失');
            $map = [
                'id' => $apply_id,
                'status' => 0,
                'insurance_id' => $insurance_id,
                'apply_status' => ['in', [1, 2]]
            ];
            $result = (new InsuranceApplyLogic)->getInfo($map);
            if($result['status']){
                if(empty($result['info'])) $this->error('error apply');
                //是否已添加支付信息
                $detail = $result['info'];
                $result = (new InsuranceOrderInfoLogic)->getInfo(['apply_id' => $apply_id, 'status' => 0]);
                if(!$result['status']) $this->error('获取保险申请信息失败');
                //支付截图
                $detail['pay_img'] = !empty($result['info']) ? $result['info']['pay_img'] : 0;

                $this->success('获取保险申请信息成功', null, $detail);
            }else{
                $this->error('获取保险申请信息失败', null, $result['info']);
            }

        }
    }

    /*
     * 保险详情
     */
    public function ApplyDetail(){

        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');
            $insurance_id = $this->_param('insurance_id');
            if(empty($insurance_id)) $this->error('保险类型id缺失');
            $map = [
                'id' => $apply_id,
                'status' => 0,
                'insurance_id' => $insurance_id,
                'apply_status' => 0
            ];
            $result = (new InsuranceApplyLogic)->getInfo($map);
            if($result['status']){
                if(empty($result['info'])) $this->error('error apply');
                //是否已添加支付信息
                $detail = $result['info'];
                $result = (new InsuranceOrderInfoLogic)->getInfo(['apply_id' => $apply_id, 'status' => 0]);
                if(!$result['status']) $this->error('获取保险申请信息失败');
                $detail['has_pay_info'] = !empty($result['info']);
                //支付截图
                $detail['pay_img'] = !empty($result['info']) ? $result['info']['pay_img'] : 0;

                //是否首次购买
                $result = (new InsuranceOrderLogic)->count(['uid' => $detail['uid'], 'insurance_id' => $detail['insurance_id'], 'status' => 0]);
                if(!$result['status']) $this->error('获取保险申请列表失败', null, $result['info']);
                $detail['is_first'] = $result['info'] > 0;


                $this->success('获取保险申请信息成功', null, $detail);
            }else{
                $this->error('获取保险申请信息失败', null, $result['info']);
            }

        }
    }

    /*
     * 添加保险内容
     */
    public function addApplyOrderInfo()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');

            $pay_info = [
                'img'      => $this->_param('img', '', 'miss img'),
                'text'     => $this->_param('text', '', 'miss text'),
                'qrcode' => $this->_param('qrcode', '', 'miss code'),
                'price'    => $this->_param('price', '', 'miss price'),
            ];
            $order_info = (new InsuranceOrderInfoLogic())->getInfo(['apply_id'=>$apply_id]);
            if(!empty($order_info['info'])){
                $result = (new InsuranceOrderInfoLogic)->save(['apply_id'=>$apply_id], ['pay_info'=>json_encode($pay_info, JSON_UNESCAPED_UNICODE)]);
            }else{
                $result = (new InsuranceOrderInfoLogic)->addInfo($apply_id, $pay_info);
            }

            if($result['status']){
                $this->success('添加修改保险内容成功', null, $result['info']);
            }else{
                $this->error($result['info'], null, $result['info']);
            }
        }
    }





    /**
     * 发送订单短信
     */
    public function sendSms()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');
            $insurance_id = $this->_param('insurance_id');
            if(empty($insurance_id)) $this->error('保险类型id缺失');

            if($insurance_id != 5) {
                $this->error('操作失败，不支持的类型', null);
            }

            $map = [
                'id' => $apply_id,
                'status' => 0,
                'insurance_id' => $insurance_id,
                'apply_status' => 0
            ];
            $result = (new InsuranceApplyLogic)->getInfo($map);
            if($result['status']){
                if(empty($result['info'])) $this->error('error apply');
                //是否已添加支付信息
                $detail = $result['info'];
                $result = (new InsuranceOrderInfoLogic)->getInfo(['apply_id' => $apply_id, 'status' => 0]);
                if(!$result['status']) $this->error('获取保险申请信息失败');
                if(empty($result['info'])) $this->error('未填写保险内容', null);

                $apply_info = $detail['apply_info'];
                if(empty($apply_info)) $this->error('申请内容缺失，操作失败', null);

                $apply_info = json_decode($apply_info, true);
                $mobile = $apply_info['mobile'];
                $name = $apply_info['name'];
                $url = WEB_URL.'/web.php/staticpage/insurance_car/id/'.$apply_id;
                $url = (new ShortLinkHelper)->sinaShortenUrl($url);

                $desc = '链接';
                $code = '尊敬的有车有保会员'.$name.'： '.$url.' ';
                $content = '点击进入后购买，谢谢惠顾';

                //发送短信
                $msg = new MessageEntity;
                $tplValue = "#code#=$code&#desc#=$desc&#content#=$content";
                $msg->setMobile($mobile);
                $msg->setTplId("48095");
                $msg->setTplValue($tplValue);
                $ret = (new JuheSms(MessageConfig::getExtraCfg()))->init($msg)->send();
                if($ret['status']){
                    $this->success('短信已发送', null, $ret['info']);
                }else{
                    $this->error($ret['info'], null, $ret['info']);
                }
            }else{
                $this->error('操作失败', null, $result['info']);
            }
        }
    }

    /*
     * 保险否决
     */
    public function applyReject()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');
            $result = (new InsuranceAction)->option(UID ,$apply_id, 2);
            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error('操作失败', null, $result['info']);
            }

        }
    }

    /**
     * 保险通过
     */
    public function applyPass()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限');
            }
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('保险申请id缺失');
            $result = (new InsuranceAction)->option(UID ,$apply_id, 1);
            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error('操作失败'.$result['info'], null, $result['info']);
            }

        }
    }

//    /*
//     * 填写保险公司反馈信息
//     */
//    public function payInfo(){
//        $apply_id=$this->_param('apply_id','');
//        if(!$this->_param('img','')){
//            return $this->boye_display();
//        }else{
//            $pay_info=[
//                'img'=>$this->_param('img','','miss img'),
//                'text'=>$this->_param('text','','miss text'),
//                'two_code'=>$this->_param('two_code','','miss code'),
//                'price'=>$this->_param('price','','miss price'),
//            ];
//
//            //debug
//            $apply_id=4;
//            $pay_info=['img'=>4,'text'=>'保险回传测试','two_code'=>'4','amount'=>'111'];
//            //
//
//            $result=(new InsuranceOrderInfoLogic())->addInfo($apply_id,$pay_info);
//
//            if($result['status']){
//                $this->success('添加成功','Insurance/ApplyList');
//            }else{
//                $this->error($result['info']);
//            }
//        }
//    }
//
//
//    /*
//     * 保险公司确认订单
//     * status ,1通过，2未通过
//     */
//    //public function ApplySure(){
//    public function test(){
////        $apply_id=$this->_param('apply_id','');
////        $status  = $this->_param('status','');
//
//        //debug
//        $apply_id=4;
//        $status=1;
//        //
//
//        $result=(new InsuranceAction())->option(UID,$apply_id,$status);
//
//        dump($result);
//
//    }

    /**
     * 订单列表
     */
    public function queryOrder()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];

            $map = null;
            $order = ['create_time' => 'desc'];
            $result = (new InsuranceOrderLogic)->queryWithCount($map, $page, $order);
            if($result['status']){
                $orders = $result['info'];
                $list = [];
                $InsuranceLogic = new InsuranceLogic;
                $VUserInfoLogic = new VUserInfoLogic;
                foreach ($orders['list'] as $order){
                    //保险类型
                    $result = $InsuranceLogic->getInfo(['id' => $order['insurance_id']]);
                    if(!$result['status']) $this->error($result['info']);
                    $order['insurance_name'] = $result['info']['name'];

                    //用户昵称、账号
                    $result = $VUserInfoLogic->getInfo(['id' => $order['uid']]);
                    if(!$result['status']) $this->error($result['info']);
                    $user_info = $result['info'];
                    $order['user_nickname'] = empty($user_info) ? '未知' : $user_info['nickname'];
                    $order['user_mobile'] = empty($user_info['mobile']) ? '未知' : $user_info['mobile'];
                    $list[] = $order;
                }
                $orders['list'] = $list;
                $this->success('获取车险订单成功', null, $orders);
            }else{
                $this->error('获取车险订单列表失败', null, $result['info']);
            }
        }
    }

    /**
     * 车险订单详情+支付信息
     */
    public function orderInfo()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }
        }

        $order_id = $this->_param('order_id');
        if(empty($order_id)) $this->error('车险订单id 缺失');
        $map = [
            'id' => $order_id
        ];
        $result = (new InsuranceOrderLogic)->getInfo($map);
        if(!$result['status']) $this->error($result['info']);
        if(empty($result['info'])) $this->error('error order');
        $order_info = $result['info'];
        //保险类型
        $result = (new InsuranceLogic)->getInfo(['id' => $order_info['insurance_id']]);
        if(!$result['status']) $this->error($result['info']);
        $order_info['insurance_name'] = $result['info']['name'];

        //用户昵称、账号
        $result = (new VUserInfoLogic)->getInfo(['id' => $order_info['uid']]);
        if(!$result['status']) $this->error('获取车险订单详情失败');
        $user_info = $result['info'];
        $order_info['user_nickname'] = empty($user_info) ? '未知' : $user_info['nickname'];
        $order_info['user_mobile'] = empty($user_info['mobile']) ? '未知' : $user_info['mobile'];

        //支付信息
        $map = [
            'order_id' => $order_info['id']
        ];
        $result = (new InsuranceOrderInfoLogic)->getInfo($map);
        if(!$result['status']) $this->error('获取车险订单详情失败');
        $order_info['pay_info'] = $result['info'];

        $this->success('获取车险订单详情成功', null, $order_info);

    }

}